#! /bin/bash
# $Id$
#
# bootstrap: Utility to easy autoconf/automake toolchain setup on
#            checkout from revision control.
#
# Copyright (c) 2002  Daniel Elstner  <daniel.elstner@gmx.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License VERSION 2 as
# published by the Free Software Foundation.  You are not allowed to
# use any other version of the license; unless you got the explicit
# permission from the author to do so.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
set -eEu
trap 'echo "This script encountered an unhandled error when trying to install autotools."' ERR

dir=$(echo "$0" | sed 's,[^/]*$,,')
test "x${dir}" = "x" && dir='.'

if test "x$(cd "${dir}" 2>/dev/null && pwd)" != "x$(pwd)"
then
    echo "This script must be executed directly from the source directory."
    exit 1
fi

# prerequisite versions
required_automake_version=1.16.1
required_autoconf_version=2.69

# try looking for tools installed previously by this script
PATH="$(pwd)/ibtk/contrib/autotools/bin:$PATH"

build_autotools=0

#############     Determine the caller asked us to build autotools     ##################
for arg in "$@"; do
    if test "x$arg" = "x--build-autotools"
    then
        echo "building autotools at user request"
        build_autotools=1
    fi
done
###################     Determine if we need to build autotools     #####################
if test $build_autotools -eq 0
then
    echo "determining whether or not we need to build autotools"
    # we explicitly check for which's failure in the next statement so disable
    # error checking in this line
    autoconf=$(which autoconf 2>/dev/null) || true
    if test "x$autoconf" != "x"
    then
        if test -x "$autoconf"
        then
            detected_autoconf_version=$("$autoconf" --version | grep -m 1 'GNU Autoconf' | cut -d" " -f4)
            echo "Found autoconf executable $autoconf with version $detected_autoconf_version"
            if test "$detected_autoconf_version" != "$required_autoconf_version"
            then
                echo "Autoconf version $detected_autoconf_version too old, building a new one..."
                build_autotools=1
            fi
        else
            echo "Unable to run autoconf."
            build_autotools=1
        fi
    else
        echo "Unable to find autoconf."
        build_autotools=1
    fi


    echo "determining whether or not we need to build libtool"
    libtool=$(which glibtool 2>/dev/null) || true
    if test "x$libtool" = "x"
    then
        libtool=$(which libtool 2>/dev/null) || true
    fi
    if test "x$libtool" != "x"
    then
        if test -x "$libtool"
        then
            detected_libtool_version=$("$libtool" --version | grep -m 1 'GNU libtool' | cut -d" " -f4)
            echo "Found libtool executable $libtool with version $detected_libtool_version"
            case "$detected_libtool_version" in
                *2.4* | *2.2*)
                    : # don't do anything if we match
                    ;;
                *)
                    echo "Libtool version $libtool_version doesn't match, building a new one..."
                    build_autotools=1
            esac
        else
            echo "Unable to run libtool."
            build_autotools=1
        fi
    else
        echo "Unable to find libtool."
        build_autotools=1
    fi

    echo "determining whether or not we need to build automake"
    automake=$(which automake 2>/dev/null) || true
    if test "x$automake" != "x"
    then
        if test -x "$automake"
        then
            detected_automake_version=$("$automake" --version | grep -m 1 "GNU automake" | cut -d" " -f4)
            echo "Found automake executable $automake with version $detected_automake_version"
            if test "$detected_automake_version" != "$required_automake_version"
            then
                echo "Automake version $detected_automake_version doesn't match, building a new one..."
                build_autotools=1
            fi
        else
            echo "Unable to run automake."
            build_autotools=1
        fi
    else
        echo "Unable to find automake."
        build_autotools=1
    fi

    if test "$build_autotools" -eq 1
    then
        echo ""
        echo "This script was either unable to detect an installation of autotools"
        echo "or the detected versions of autotools don't match the required versions."
        echo "To reduce changes in generated output files, IBAMR requires specific"
        echo "versions (automake 1.16.1, autoconf 2.69, and libtool 2.2 or 2.4) of"
        echo "each component."
        echo ""
    fi
fi

###################          If necessary, build autotools          #####################
if test "$build_autotools" -eq 1
then
    top_srcdir=$(pwd)
    autotools_distdir=$top_srcdir/ibtk/contrib/autotools

    if test -d "$autotools_distdir"
    then
        # set the path to use the new tools as they are installed.
        # for example, the new autoconf is needed to build automake.
        PATH=$autotools_distdir/bin:$PATH
        for tool in autoconf-$required_autoconf_version automake-$required_automake_version libtool-2.4.6 ; do
            cd "$autotools_distdir"
            echo "  building $tool in $autotools_distdir"
            tar zxf $tool.tar.gz
            cd $tool && ./configure --prefix="$autotools_distdir" >/dev/null && make install >/dev/null
            cd .. && rm -rf $tool
        done

        cd "$top_srcdir"

        if test -x "$autotools_distdir/bin/autoreconf" -a -x "$autotools_distdir/bin/automake" \
                -a -x "$autotools_distdir/bin/libtool"
        then
            echo " "
            echo " --> successfully installed autoreconf in $autotools_distdir/bin - please update your PATH and try again!"
            echo " "
            PATH="$autotools_distdir/bin:$PATH" "./$0"
            exit 0
        fi
    fi
fi

###################            Finally, rerun autoreconf            #####################
autoreconf=$(which autoreconf 2>/dev/null)
# prefer autoreconf when it is available
if test "x$autoreconf" != "x"
then
    if test -x "$autoreconf"
    then
        "$autoreconf" --force --verbose --install -I config -I m4
        exit $?
    fi
fi

# # now recursively call bootstrap with the autotools in our path
# export PATH=$autotools_distdir/bin:$PATH
# which autoreconf
# $0
